import Kangaroo from './kangaroo'

const customEventName = 'cutomhashchange'
const states = ['pushState', 'replaceState']

export default function hashChangeEvent(func: Function): Kangaroo {
  const kangaroo = new Kangaroo()
  states.forEach(function (val) {
    const fun = history[val]
    history[val] = function (query, title, url) {
      const params = Array.apply(null, arguments)
      const href = location.href
      fun.apply(history, params)
      if (typeof url !== 'string' || url === href) return
      kangaroo.custom(customEventName, url)
    }
  })
  kangaroo.add('hashchange', func)
  kangaroo.add(customEventName, func)

  return kangaroo
}
